import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import '../constant/constant.dart';
import 'worm/worm.dart';
import '../page/page2.dart';
import '../page/page3.dart';

/// 首页
class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.grey,
      ),
      home: HomePage(title: 'Flutter'),
    );
  }
}

/// 首页
class HomePage extends StatefulWidget {
  /// 标题
  final String title;

  /// 构造方法
  HomePage({Key key, @required this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() => HomePageState();
}

/// 主页状态
class HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  /// 策划动画
  static SlideTransition createTransition(
      Animation<double> animation, Widget child) {
    return new SlideTransition(
      position: new Tween<Offset>(
        begin: const Offset(1.0, 0.0),
        end: const Offset(0.0, 0.0),
      ).animate(animation),
      child: child,
    );
  }

  /// Tab控制器
  TabController _controller;

  /// 当前位置
  int _currentIndex = 0;

  /// 状态页面列表
  List _children = [
    new WormPage(title: '图虫'),
    new Page2Page(title: '2'),
    new Page3Page(title: '3')
  ];

  @override
  void initState() {
    super.initState();
    // 初始化控制器
    _controller = new TabController(length: 3, vsync: this);
  }

  @override
  void dispose() {
    // 销毁控制器
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 设置状态栏可见
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      drawer: Drawer(
        child: ListView(
          children: <Widget>[
            UserAccountsDrawerHeader(
              accountName: Text('麻再挺',
                  style: TextStyle(
                      fontFamily: Constant.FONT, color: Colors.black45)), // 用户名
              accountEmail: Text('zaitingma@foxmail.com',
                  style: TextStyle(
                      fontFamily: Constant.FONT, color: Colors.black45)), // 邮箱
              currentAccountPicture: CircleAvatar(
                backgroundImage: AssetImage(Constant.AVATAR), // 头像
              ),
              decoration: BoxDecoration(
                  image: DecorationImage(
                      fit: BoxFit.cover,
                      image: AssetImage(Constant.DRAWER_HEADER_BG))),
            ),
            new ListTile(
              title:
                  new Text("设置", style: TextStyle(fontFamily: Constant.FONT)),
              trailing: new Icon(Icons.chevron_right),
            ), // 侧滑头
          ],
        ),
      ), // 侧滑
      /// TabBarView + TabBar
      // body: TabBarView(controller: _controller, children: <Widget>[
      //   new WormPage(title: '1'),
      //   new Page2Page(title: '2'),
      //   new Page3Page(title: '3')
      // ]),
      // bottomNavigationBar: Material(
      //   color: Colors.white,
      //   child: TabBar(
      //       controller: _controller,
      //       indicatorColor: Colors.transparent, // 设置底部指示器透明
      //       labelColor: Colors.red,
      //       unselectedLabelColor: Colors.black26,
      //       tabs: <Widget>[
      //         Tab(
      //           text: '图虫',
      //           icon: Icon(Icons.collections),
      //         ),
      //         Tab(
      //           text: '课表',
      //           icon: Icon(Icons.map),
      //         ),
      //         Tab(
      //           text: '操场',
      //           icon: Icon(Icons.directions_run),
      //         )
      //       ]),
      // ),
      /// BottomNavigationBar + BottomNavigationBarItem
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            this._currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            title: Text('图虫'),
            icon: Icon(Icons.collections),
          ),
          BottomNavigationBarItem(
            title: Text('图虫'),
            icon: Icon(Icons.collections),
          ),
          BottomNavigationBarItem(
            title: Text('图虫'),
            icon: Icon(Icons.collections),
          )
        ],
      ),
    );
  }

  /// 获取对应的图标
  // Image getTabIcon(int curIndex) {
  //   if (curIndex == this._currentIndex) {
  //     return tabImages[curIndex][1];
  //   }
  //   return tabImages[curIndex][0];
  // }
}
